//
//  BRWRentalOrderVC.m
//  Kexu
//
//  Created by IOS on 2023/7/31.
//

#import "BRWRentalOrderVC.h"
#import "BRRentalOrderListCell.h"

@interface BRWRentalOrderVC ()<QMUITableViewDelegate,QMUITableViewDataSource>
{
    NSInteger _offset;
}
@property (nonatomic,strong) NSMutableArray *dataArr;

@end

@implementation BRWRentalOrderVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.dataArr = [NSMutableArray array];
    
    [self setUI];
    
    [self addRefreshActionForView:self.tableView];
    [self addLoadMoreActionForView:self.tableView];
    
    self.tableView.mj_footer.hidden = YES;
    
    [self requestList];
    
}

- (void)handleListRefreshAction {
    _offset = 0;
    [self requestList];
}

- (void)handleListLoadMoreAction {
    _offset += 20;
    [self requestList];
}


- (void)requestList {
    
    [self.tableView ly_startLoading];
    
    [[BRRequestManager  shared] GET:[BRAPI merchantRentOrder] params:@{@"offset":@(_offset),@"branchId":USERManager.branchId} succeed:^(NSDictionary * _Nonnull dataDict, NSInteger respCode) {
        
        
        if (self->_offset  == 0) {
            [self.dataArr removeAllObjects];
            self.tableView.mj_footer.hidden = NO;
        }
        NSInteger oldCount = self.dataArr.count;
        NSArray *arr = [BROrderListModel mj_objectArrayWithKeyValuesArray:dataDict[@"data"]];
       
//        [self.dataArr arrayByAddingObjectsFromArray:arr];
        [self.dataArr addObjectsFromArray:arr];
        
        if (self.dataArr.count < oldCount + kPageSize || self.dataArr.count == 0 ) {
            self.tableView.mj_footer.hidden = YES;
        }
        
        [self.tableView reloadData];
        [self showBREmptyView];
        [self handleEndRefreshForView:self.tableView];
        
    } failed:^(NSInteger code, NSString * _Nonnull errMsg) {
        [self showBRErrorView];
        [self handleEndRefreshForView:self.tableView];
    }];
}


#pragma mark - tableView Delegate --

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return self.dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    BRRentalOrderListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BRRentalOrderListCell"];

    cell.dto = self.dataArr[indexPath.row];
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    

    
}

#pragma mark - UI --
- (void)setUI {
    
    [self br_createTop:Localized(@"shop_rentalOrder")];
    
    self.tableView.estimatedRowHeight = 134;
    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = UIColor.clearColor;
    [self.tableView registerClass:[BRRentalOrderListCell class] forCellReuseIdentifier:@"BRRentalOrderListCell"];
    
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    
    UIView *head = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 14)];
    self.tableView.tableHeaderView = head;
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
